import { getDicts as getDicts } from '@/api/system/dict/data'
import { listDatasourceInfo } from '@/api/console/datasourceInfo'
import typeStatus from '@/assets/images/字段映射.png'
import monitorStatus from '@/assets/images/任务调度.png'
import batchStatus from '@/assets/images/同步设置.png'
import mappingStatus from '@/assets/images/映射关系配置.png'
import syncStatus from '@/assets/images/同步配置.png'
import {
  getAiList
} from '@/api/console/belfAi'

function sort(data) {
  const sortedData = Object.entries(data).sort((a, b) => {
    const [, valueA] = a
    const [, valueB] = b
    return valueA.split('-')[0].localeCompare(valueB.split('-')[0])
  })
  const result = Object.fromEntries(sortedData)
  return result
}

async function getTaskType() {
  const arr = []
  const res = await getDicts('data_base_type')
  res.data.forEach(f => {
    arr.push({
      value: f.dictValue,
      label: f.dictLabel
    })
  })
  return arr
}

async function getDataSourceList(info) {
  const arr = []
  const data = await listDatasourceInfo(info)
  data.rows.forEach(f => {
    arr.push({
      label: f.dataSourceName,
      value: f.id,
      ...f
    })
  })
  return arr
}

const stepOneData = [{
  label: '任务名称',
  dataName: 'taskName',
  component: 'input',
  type: 'text',
  span: 20,
  labelWidth: '100px',
  width: '75%'
}, {
  label: '任务描述',
  dataName: 'dsc',
  component: 'input',
  type: 'textarea',
  span: 20,
  labelWidth: '100px',
  width: '75%',
  rules: [
    {
      required: false,
      message: `请输入任务描述`,
      trigger: 'change'
    }
  ]
},
{
  label: '任务类型',
  dataName: 'jobType',
  component: 'cascader',
  span: 20,
  labelWidth: '100px',
  width: '75%',
  defaultValue: ['common', 'SYNC_STRUCTURE'],
  option: async function(type) {
    const arr = [
      {
        value: 'common',
        label: '常用',
        children: []
      },
      {
        value: 'basic',
        label: '基本',
        children: []
      }
    ]
    const res = await getDicts('synchronous_tasktype')
    res.data.forEach((f) => {
      arr[f.remark === 'common' ? 0 : 1].children.push({
        value: f.dictValue,
        label: f.dictLabel
      })
    })
    return arr
  }
}, {
  label: 'AI源',
  dataName: 'aiModel',
  component: 'select',
  span: 20,
  labelWidth: '100px',
  async option() {
    const arr = []
    const res = await getAiList()
    Object.keys(sort(res)).forEach((f) => {
      arr.push({
        label: `${res[f]}(${f})`,
        value: f
      })
    })
    return arr
  }
}, {
  label: '任务模式',
  dataName: 'start',
  component: 'select',
  span: 20,
  labelWidth: '100px',
  width: '40%',
  dict: 'synchronous_exceptionhandling',
  option: getTaskType,
  border: true,
  defaultValue: 'MYSQL',
  inlineEl: [
    {
      dataName: 'end',
      component: 'select',
      width: '40%',
      labelWidth: '0px',
      defaultValue: 'DAMENG',
      dict: 'synchronous_exceptionhandling',
      option: getTaskType
    }
  ]
},
{
  label: '工作模式',
  dataName: 'jobMode',
  component: 'radio',
  span: 20,
  labelWidth: '100px',
  width: '75%',
  dict: 'synchronous_jobmode',
  defaultValue: 'ONCE',
  border: true
  // aiModel
}, {
  label: '异常处理',
  dataName: 'taskPolicy',
  component: 'radio',
  span: 20,
  labelWidth: '100px',
  width: '75%',
  dict: 'synchronous_exceptionhandling',
  border: true,
  defaultValue: 'ERROR_CONTINUE'
}, {
  label: '任务状态',
  dataName: 'status',
  component: 'radio',
  span: 20,
  labelWidth: '100px',
  width: '75%',
  dict: 'sys_job_status',
  border: true,
  defaultValue: '0'
}]

const stepTwoData = [{
  label: '起始实例名',
  dataName: 'sourceId',
  component: 'select',
  span: 11,
  labelWidth: '100px',
  option: async function(type, data) {
    const dictItem = await getDicts('database_datatype')
    const example = dictItem.data.filter((f) => f.dictLabel === '实例')
    console.log(data, '8')

    return getDataSourceList({
      type: data.start,
      dataType: example[0].dictValue,
      startNum: 0,
      pgSize: 999
    })
  }
}, {
  label: '目标实例名',
  dataName: 'sinkId',
  component: 'select',
  span: 11,
  labelWidth: '100px',
  option: async function(type, data) {
    const dictItem = await getDicts('database_datatype')
    const example = dictItem.data.filter((f) => f.dictLabel === '实例')
    return getDataSourceList({
      type: data.end,
      dataType: example[0].dictValue,
      startNum: 0,
      pgSize: 999
    })
  }
}]

const stepThreeData = [
  {
    label: '源库',
    dataName: 'sourceDatabase',
    component: 'select',
    span: 11,
    labelWidth: '110px',
    async option() {
      return []
    }
  },
  {
    label: '源schema',
    dataName: 'sourceDataSchema',
    component: 'select',
    span: 11,
    labelWidth: '110px',
    option() { return [] }
  },
  {
    label: '目标库',
    dataName: 'sinkDatabase',
    component: 'select',
    span: 11,
    labelWidth: '110px',
    option() { return [] }
  },
  {
    label: '目标schema',
    dataName: 'sinkDataSchema',
    component: 'select',
    span: 11,
    labelWidth: '110px',
    option() { return [] }
  }
]

const stepFourStatus = [{
  label: '字段映射',
  key: 'typeStatus',
  dsc: '字段映射配置是将源数据库中的字段与目标数据库中的字段进行对应和转换的过程。',
  img: typeStatus,
  confName: ['dbTypeMapping', 'tableTypeMapping'],
  show(data) {
    return data.jobType[1] === 'SYNC_STRUCTURE' || data.jobType[1] === 'CREATE_TABLE'
  }
}, {
  label: '同步配置',
  key: 'syncStatus',
  dsc: '同步配置是一套用于控制和管理源数据库与目标数据库之间数据同步过程的规则和设置。',
  img: syncStatus,
  confName: ['syncConf'],
  show(data) {
    return data.jobMode === 'STREAMING'
  }
}, {
  label: '批量同步配置',
  key: 'batchStatus',
  dsc: '批量同步配置是一种用于控制数据从源数据库批量传输到目标数据库的设置。注：',
  tips: '需在数据库迁移对象中选择源表',
  img: batchStatus,
  confName: ['jdbc'],
  show(data) {
    return data.jobMode === 'BATCH'
  }
}, {
  label: '映射关系配置',
  key: 'mappingStatus',
  dsc: '映射关系配置是建立源数据库和目标数据库之间数据结构和数据元素对应关系的过程。注：',
  tips: '需在数据库迁移对象中选择源表',
  img: mappingStatus,
  confName: ['mapping'],
  show(data) {
    return data.jobMode === 'BATCH' || data.jobMode === 'STREAMING'
  }
}, {
  label: '任务调度',
  key: 'monitorStatus',
  dsc: '任务调度配置在特定的时间或条件下，按照预定的顺序执行任务的过程。',
  img: monitorStatus,
  confName: ['monitorConf'],
  show(data) {
    return true
  }
}]

export {
  stepOneData,
  stepTwoData,
  stepThreeData,
  stepFourStatus
}
